<?php

    ##################################################
    #
    # Copyright (c) 2004-2013 OIC Group, Inc.
    #
    # This file is part of Exponent
    #
    # Exponent is free software; you can redistribute
    # it and/or modify it under the terms of the GNU
    # General Public License as published by the Free
    # Software Foundation; either version 2 of the
    # License, or (at your option) any later version.
    #
    # GPL: http://www.gnu.org/licenses/gpl.txt
    #
    ##################################################

    /**
    * @subpackage Models
    * @package Modules
    */
    class Quiz_detail
    {
        public $collect_id;
        public $title;
        public $type;
        public $point;
        public $point_type;
        public $diff;
        public $content;
        public $ans;
        public $create_on;
        public $active; 
        public $make_by;
        public $count;
        public $rank;    
        public function __construct($default_type=0)
        {
            $this->collect_id=0;
            $this->title="";
            $this->type=$default_type==null?0:$default_type;
            $this->diff=QUIZ_DEFAULT_DIFF;
            $this->point=0;
            $this->point_type=0;
            $this->content="";
            $this->ans="";
            $this->create_on=time();
            $this->active=1;
            $this->make_by=0;
            $this->count=0;
            $this->rank=0;
        }

    }
    class Quiz_collect
    {
        public $name;
        public $default_type;
        public $description;
        public $make_by;
        public $create_on;
        public $modify_on;
        public $active;
        public $permiss;
        public $count_view;
         public function __construct()
        {
            $this->default_type=0;
            $this->description="";
            $this->make_by=0;
            $this->create_on=time();
            $this->modify_on=0;
            $this->active=1;
            $this->permiss=0;
            $this->count_view=0;            
        }
    }

    class quiz extends expRecord {
        //public $table = 'photo';
        public $validates = array(
        'presence_of'=>array(
        'title'=>array('message'=>'Title is a required field.'),
        //'body'=>array('message'=>'Body is a required field.'),
        ));
        protected $attachable_item_types = array(
        'content_expFiles'=>'expFile',
        'content_expCats'=>'expCat',
        'content_expTags'=>'expTag'
        );

        public function addNextPrev($where=1) {
            global $db;

            $maxrank = $db->max($this->tablename,'rank','',$where);

            //        $this->next = $db->selectValue($this->tablename,'sef_url',$where." AND rank=".($this->rank+1));
            $next = $this->find('first',$where." AND rank>".($this->rank));
            if (!empty($next)) $this->next = $next->sef_url;

            //        $this->prev = $db->selectValue($this->tablename,'sef_url',$where." AND rank=".($this->rank-1));
            $prev = $this->find('first',$where." AND rank<".($this->rank));
            if (!empty($prev)) $this->prev = $prev->sef_url;

            //        if ($this->rank==$maxrank) {
            //            $where = $where." AND rank=1";
            //            $this->next = $db->selectValue($this->tablename,'sef_url',$where);
            //        }
            if (empty($this->next)) {
                $next = $this->find('first',$where,'rank ASC');
                $this->next = $next->sef_url;
            }

            //        if ($this->rank==1) {
            //            $where = $where." AND rank=".$maxrank;
            //            $this->prev = $db->selectValue($this->tablename,'sef_url',$where,'rank');
            //        }
            if (empty($this->prev)) {
                $prev = $this->find('first',$where,'rank DESC');
                $this->prev = $prev->sef_url;
            }
        }

        public function post_quiz_from_string($string,$coll,$id)
        {
            
            global $db,$user;
            if($id>0)
                $db->delete('quiz','collect_id='.$id.' AND make_by='.$user->id);
            $quizzes=array();
            $logs=array();
            $string = str_replace("\r\n",'', $string);

            $string = str_replace('<div>','<br>', $string);
            $string=strip_tags($string,"<b><i><strike><u>");
            if(strlen($string)>100000)
            {
                return "Failure, max char is 100.000";
            }        
            $quizzes_str = explode(QUIZ_NEW, $string);
            if(sizeof($quizzes_str)>100)
            {
                return "Failure, max quizzes is 100";
            }
            foreach($quizzes_str as $key=>$quiz_str)
            {
                if(strlen(trim($quiz_str))<10)
                    continue;
                $quiz_strs=explode(QUIZ_ANS, $quiz_str);
                $quiz= new Quiz_detail($coll->default_type) ;
                $quiz->collect_id=$coll->id;
                $quiz->make_by=$user->id;
                $ans_array=array();
                foreach($quiz_strs as $key_ans=>$ans_str)
                {
                    if($key_ans==0)
                    {
                        $matches_pont = array();
                        $s = preg_match('/'.QUIZ_POINT.'([0-9]+)/', $ans_str, $matches_pont);
                        $ans_str = str_replace($matches_pont[0],'', $ans_str);
                        $quiz->point=$matches_pont[1];
                        //--DIFF
                        $matches_pont_d = array();
                        $s_d = preg_match('/'.QUIZ_DIFF.'([0-9]+)/', $ans_str, $matches_pont_d);
                        $ans_str_d = str_replace($matches_pont_d[0],'', $ans_str);
                        $quiz->diff=$matches_pont_d[1];
                        //----
                        $quiz->content= trim($ans_str_d);
                        continue;
                    }
                    if(strpos($ans_str, QUIZ_RIGHT)>0)
                    {
                        $quiz->right=$key_ans;
                        /*
                        *$this->collect_id=0;
                        $this->title="";
                        $this->type=0;
                        $this->point=0;
                        $this->point_type=0;
                        $this->content="";
                        $this->ans="";
                        $this->create_on=time();
                        $this->active=1;
                        $this->make_by=0;
                        $this->count=0;
                        $this->rank=0; 
                        */
                    }
                    $ans_str = str_replace('<br>','', $ans_str);
                    $ans_array[]=trim($ans_str);
                }
                $quiz->ans=json_encode($ans_array);
                $db->insertObject($quiz,'quiz');
                //$quizzes[]=$quiz;
            }
            return true;
        }
    }

?>